package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Course;
import com.shixulin.entity.DTO.CourseQueryDTO;
import com.shixulin.mapper.CourseInfoMapper;
import com.shixulin.mapper.CourseMapper;
import com.shixulin.service.CourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, Course> implements CourseInfoService {

    @Autowired
    CourseMapper courseMapper;
    @Override
    public ResponseResult getCourseInfoByPage(CourseQueryDTO queryDTO) {

//        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("teacher_id",queryDTO.getTeacherId());
//        List<Course>courseInfos = courseMapper.selectList(queryWrapper);
//
//        Map<String, Object> map = new HashMap<>();
//
//        map.put("data",baseMapper.getCourseInfoByPage(queryDTO));
//        map.put("total",courseInfos.size());
//
//        // 处理分页逻辑，调用Mapper中的方法
//        return  new ResponseResult<>(300,"查询成功",map);

        //分页参数
        Page<Course> rowPage = new Page(queryDTO.getCurrent(), queryDTO.getSize());

        //queryWrapper组装查询where条件
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getTeacherId, queryDTO.getTeacherId());
        rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
        return new ResponseResult(300,"查询成功",rowPage);

    }

    @Override
    public ResponseResult getCourseData(int subjectId) {

        try {
          LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
          queryWrapper.eq(Course::getSubjectId,subjectId);
          List<Course>CourseData = this.baseMapper.selectList(queryWrapper);
          if (null != CourseData&& !CourseData.isEmpty()) {
              return new ResponseResult(300,"查询成功",CourseData);
          }else {
              return new ResponseResult(505,"没有数据");
          }

        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(405,"错误");
        }

    }

    // 其他方法...

}
